package com.iconmaker.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileManager {
	private static FileManager sInstance;
	private static final int BUFFER_SIZE = 8092;
	
	public static FileManager getInstance() {
		if(sInstance == null)
			sInstance = new FileManager();
		
		return sInstance;
	}
	
	private FileManager() {}
	
	public boolean copy(String from, String to) {
		File fromFile = new File(from);
		
		if(!fromFile.exists()) {
			return false;
		}
		
		to += File.separator + fromFile.getName();
		
		if(fromFile.isDirectory()) {
			File toFile = new File(to);
			
			if(!toFile.exists())
				toFile.mkdirs();
			
			File[] childList = fromFile.listFiles();
			for(File child : childList) {
				return copy(child.getAbsolutePath(), to);
			}
		}
		else {
			return copyFile(fromFile, doCreateNewFile(to));
		}
		
		return true;
	}
	
	public boolean copyFile(File fromFile, File toFile) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(fromFile), BUFFER_SIZE);
			bos = new BufferedOutputStream(new FileOutputStream(toFile), BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int length;
			
			while((length = bis.read(buffer)) != -1) {
				bos.write(buffer, 0, length);
			}
			
			return true;
			
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			if(bis != null) {
				try {
					bis.close();
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			if(bos != null) {
				try {
					bos.close();
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return false;
	}
	
	public boolean createNewFile(String path) {
		File file = doCreateNewFile(path);
		
		if(file.exists())
			return true;
		else
			return false;
	}
	
	private File doCreateNewFile(String path) {
		File rootDir = null;
		StringBuilder parentsDirs = new StringBuilder();
		String[] splitedParentDirs = path.split("/");
		String fileName = splitedParentDirs[splitedParentDirs.length - 1];
		
		for(int i = 0; i < splitedParentDirs.length - 1; i++) {
			parentsDirs.append(splitedParentDirs[i]);
			parentsDirs.append(File.separator);
		}
		
		rootDir = new File(parentsDirs.toString());
		
		if(!rootDir.exists())
			rootDir.mkdirs();
		
		File file = new File(rootDir, fileName);
		
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		return file;
	}
	
	public boolean deleteFile(String path) {
		File file = new File(path);
		
		if(file.exists()) {
			if(file.isDirectory()) {
				File[] childList = file.listFiles();
				
				for(File child : childList)
					deleteFile(child.getAbsolutePath());
			}
			
			return file.delete();
		}
		
		return false;
	}
	
	public boolean exists(String path) {
		File file = new File(path);
		
		return file.exists();
	}
	
//	public String getStoragePath(boolean onPrivate) {
//		String path = "";
//		
//		if(onPrivate) {
//			path = AlopexManager.getCurrentActivity().getFilesDir().getAbsolutePath();
//		}
//		else {
//			if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//				path = Environment.getExternalStorageDirectory().getAbsolutePath();
//			}
//		}
//		
//		return path;
//	}
	
	public boolean isDirectory(String path) {
		File file = new File(path);
		
		if(file.exists()) {
			return file.isDirectory();
		}
		
		return false;
	}
	
	public File getFile(String path) {
		File file = new File(path);
		
		if(file.exists()) {
			return file;
		}
		else {
			return null;
		}
	}
	
	public boolean mkdirs(String path) {
		File file = new File(path);
		
		if(!file.exists()) {
			return file.mkdirs();
		}
		
		return true;
	}
	
	public boolean move(String from, String to) {
		if(copy(from, to)) {
			boolean i = deleteFile(from);
			return i;
		}
		
		return false;
	}
	
	public boolean rename(String path, String name) {
		File oldFile = new File(path);
		String[] splitedPath = path.split("/");
		path = path.replace(splitedPath[splitedPath.length - 1], "");
		
		if(oldFile.exists()) {
			return oldFile.renameTo(new File(path + name));
		}
		
		return false;
	}
}